package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.Examination;
import cn.wolfcode.crm.query.ExaminationQueryObject;
import cn.wolfcode.crm.service.IExaminationService;
import cn.wolfcode.crm.util.JsonResult;
import cn.wolfcode.crm.util.PageResult;
import cn.wolfcode.crm.util.PermissionName;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("examination")
public class ExaminationController {

    @Autowired
    private IExaminationService examinationService;

    @PermissionName("考试管理首页")
    @RequiresPermissions("examination:index")
    @RequestMapping("index")
    public String index(){
        return "examination/list";
    }

    @PermissionName("考试管理列表")
    @RequiresPermissions("examination:list")
    @ResponseBody
    @RequestMapping("list")
    public PageResult list(ExaminationQueryObject qo){
        /*应该会客户的对象并带有*/
        PageResult result = examinationService.query(qo);
        return result;
    }

    @PermissionName("考试管理保存")
    @RequiresPermissions("examination:save")
    @ResponseBody
    @RequestMapping("save")
    public JsonResult save(Examination exam){

        JsonResult result = new JsonResult();
        try {
//            考试管理的保存不能修改到客户的信息，有关客户的信息都是只读的
            examinationService.insert(exam);
        }catch (Exception e){
            e.printStackTrace();
            result = new JsonResult("保存出错!");
        }
        return result;
    }

    @PermissionName("考试管理编辑")
    @RequiresPermissions("examination:edit")
    @ResponseBody
    @RequestMapping("edit")
    public JsonResult edit(Examination edit){

        JsonResult result = new JsonResult();
        try {
//            考试管理的保存不能修改到客户的信息，有关客户的信息都是只读的
            examinationService.updateByPrimaryKey(edit);
        }catch (Exception e){
            e.printStackTrace();
            result = new JsonResult("编辑出错!");
        }
        return result;
    }

    @PermissionName("考试管理删除")
    @RequiresPermissions("examination:delete")
    @ResponseBody
    @RequestMapping("delete")
    public JsonResult delete(Long id){

        JsonResult result = new JsonResult();
        try {
//            考试管理的保存不能修改到客户的信息，有关客户的信息都是只读的
            examinationService.deleteByPrimaryKey(id);
        }catch (Exception e){
            e.printStackTrace();
            result = new JsonResult("删除失败!");
        }
        return result;
    }

    @PermissionName("考试管理审核")
    @RequiresPermissions("examination:changeState")
    @ResponseBody
    @RequestMapping("changeState")
    public JsonResult changeState(Examination record){

        JsonResult result = new JsonResult();
        try {
//            考试管理的保存不能修改到客户的信息，有关客户的信息都是只读的
            examinationService.changeState(record);
        }catch (Exception e){
            e.printStackTrace();
            result = new JsonResult("操作失败!");
        }
        return result;
    }
}
